package com.example.gitpart03;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.gitpart03.Type.TypeFragment;

public class MainActivity extends AppCompatActivity {

    private static final String TAG_DINGDAN_FRAGMENT = "DingDanFragment";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            replaceFragment(new ShouYeFragment());
        }

        // 处理从 Login 传递过来的登录成功标志位
        boolean isLoggedIn = getIntent().getBooleanExtra("isLoggedIn", false);

        // 处理从 ThirdPage 传递过来的产品信息
        Bundle extras = getIntent().getExtras();
        if (extras != null && extras.getBoolean("showDingDanFragment", false)) {
            String productName = extras.getString("productName");
            int productImageResId = extras.getInt("productImageResId");

            DingDanFragment dingDanFragment = (DingDanFragment) getSupportFragmentManager().findFragmentByTag(TAG_DINGDAN_FRAGMENT);
            if (dingDanFragment == null) {
                dingDanFragment = new DingDanFragment();
            }
            dingDanFragment.addOrder(productName, productImageResId);

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, dingDanFragment, TAG_DINGDAN_FRAGMENT);
            transaction.commit();
        }

        // 找到底部导航栏的“我的”选项
        LinearLayout profile = findViewById(R.id.profile);
        profile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 MyselfFragment 实例
                MyselfFragment myselfFragment = new MyselfFragment();
                Bundle args = new Bundle();
                args.putBoolean("isLoggedIn", isLoggedIn); // 传递登录成功标志位
                myselfFragment.setArguments(args);
                // 开启 Fragment 事务
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                // 替换内容区域的 Fragment
                transaction.replace(R.id.fragment_container, myselfFragment);
                // 提交事务
                transaction.commit();
            }
        });

        // 找到底部导航栏的“首页”选项
        LinearLayout shouye = findViewById(R.id.shouye);
        shouye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 ShouYeFragment 实例
                ShouYeFragment shouYeFragment = new ShouYeFragment();
                // 开启 Fragment 事务
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                // 替换内容区域的 Fragment
                transaction.replace(R.id.fragment_container, shouYeFragment);
                // 提交事务
                transaction.commit();
            }
        });

        // 找到底部导航栏的“订单”选项
        LinearLayout dingdan = findViewById(R.id.dingdan);
        dingdan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DingDanFragment dingDanFragment = (DingDanFragment) getSupportFragmentManager().findFragmentByTag(TAG_DINGDAN_FRAGMENT);
                if (dingDanFragment == null) {
                    dingDanFragment = new DingDanFragment();
                }
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, dingDanFragment, TAG_DINGDAN_FRAGMENT);
                transaction.commit();
            }
        });

        // 找到底部导航栏的“分类”选项
        LinearLayout type = findViewById(R.id.fenlei);
        type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 TypeFragment 实例
                TypeFragment typeFragment = new TypeFragment();
                // 开启 Fragment 事务
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                // 替换内容区域的 Fragment
                transaction.replace(R.id.fragment_container, typeFragment);
                // 提交事务
                transaction.commit();
            }
        });
    }

    private void replaceFragment(ShouYeFragment shouYeFragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, shouYeFragment);
        transaction.addToBackStack(null); // 将事务添加到回退栈
        transaction.commit();
    }
}